﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - ClassroomTimeTable" 
*/ 

component 
	displayname="public.service.db.senate.sequence.ClassroomTimeTable" 
	output=false 
	accessors=true 
	{

	property type="string" name="TermID" hint="学期代码";
	property type="string" name="ClassroomID" hint="教室编号";
	property type="string" name="Index" hint="教室使用序号";

	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "生成教室使用序号 - 教室使用序号规则 4位代码"
	*/
	public string function getIDX() output=false {
		
		var nextID = numberFormat(1, "0___");
		
		var sql = "SELECT uidx FROM t_clsroom_timetable 
					WHERE 
						term_id = :termId AND cr_id = :classroomId 
					ORDER BY uidx DESC ";
		
		var queryObj = new Query( datasource=application.dnsMaster, maxRows=1 );
		
		queryObj.addParam( name="termId", value=getTermID(), cfsqltype="cf_sql_char" );
		queryObj.addParam( name="classroomId", value=getClassroomID(), cfsqltype="cf_sql_char" );

		var rs_check = queryObj.execute( sql=sql ).getResult();
		
		if ( rs_check.recordCount ) {
			/* 创建一个追加id */
			nextID = numberFormat( ( rs_check.uidx + 1 ), "0___" );
		}
		
		return nextID;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = getTermID() & "," & getClassroomID() & "," & getIndex();

		/* 
			主键生成规则 
			
			教室使用编号21位 3位学期代码 , 12位教室编号 , 4位使用序号

			Unique: TERM_ID, CR_ID, UIDX
		*/
		return nextID;
	}

}

</cfscript>